package redis;


import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.Map;

/**
 * @author vivi20
 * @ClassName RedisWithReentrantlock
 * @Date 2019/12/4 14:56
 * @Vertion 1.0
 */
public class RedisWithReentrantlock {

    private ThreadLocal<Map> lockers = new ThreadLocal<>();
    private Jedis jedis;

    public RedisWithReentrantlock(Jedis jedis) {
        this.jedis = jedis;
    }

    private boolean _lock(String key){
        return jedis.set(key,"","nx","ex",5L)!=null;
    }

    private void _unlock(String key){
        jedis.del(key);
    }

    private Map<String,Integer> currentLockers(){
        Map<String,Integer> refs = lockers.get();
        if (refs != null){
            return refs;
        }
        lockers.set(new HashMap<>());
        return lockers.get();
    }

    public boolean lock(String key){
        Map refs = currentLockers();
        Integer refCnt = (Integer) refs.get(key);
        if (refCnt != null){
            refs.put(key,refCnt+1);
        }
        boolean ok = this._lock(key);
        if (ok){
            return false;
        }
        refs.put(key,1);
        return true;
    }

    public boolean unlock(String key){
        Map refs = currentLockers();
        Integer refCnt = (Integer) refs.get(key);
        if (refCnt == null){
            return false;
        }
        refCnt -= 1;
        if (refCnt>0){
            refs.put(key,refCnt);
        }else {
            refs.remove(key);
            this._unlock(key);
        }
        return true;
    }


    public static void main(String[] args) {
        Jedis jedis = new Jedis();
        RedisWithReentrantlock redis = new RedisWithReentrantlock(jedis);
        System.out.println(redis.lock("codehole"));
        System.out.println(redis.lock("codehole"));
        System.out.println(redis.unlock("codehole"));
        System.out.println(redis.unlock("codehole"));
    }





}
